<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2022/12/7
 * Time: 17:25
 */

namespace app\controller;
use app\model\app\AppLoanOrder;
use app\model\app\AppPayOrder;

class Callback
{
    public function pay_callback(){
        $order_no = input('mchOrderNo');
        $data = json_decode(file_get_contents('php://input'),true);
        if (!empty($data)){
            $order_no = $data['merchantCode'];
            if ($data['status'] == 'FAILED')die('FAILED');
        }else{
            $tradeResult = input('tradeResult');
            if ($tradeResult != 1)die('FAILED');
        }
        $find = AppPayOrder::where('order_code',$order_no)->find();
        if ($find->is_credit == 1){
            AppLoanOrder::where('id',$find->order_id)
                ->inc('credit_amount',$find->pay_amount)
                ->update(['update_time' => now(),'is_credit' => 2]);
        }
        $find->save([
            'status' => 1,
            'is_credit' => $find->is_credit == 1 ? 2 : 0,
            'update_time' => date('Y-m-d H:i:s')
        ]);
        die('success');
    }
}